package com.feel.mall.goodsservice.service;

import com.feel.mall.common.bean.Category;
import com.feel.mall.common.bean.Goods;
import com.feel.mall.common.exception.FeelException;
import com.feel.mall.common.service.CategoryService;
import com.feel.mall.common.service.GoodsService;
import com.feel.mall.goodsservice.mapper.CategoryMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: 刘永钦
 * @Description:
 */
@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private CategoryMapper categoryMapper;

    // 添加分类方法
    @Override
    public Boolean add(Category category) throws FeelException {
        // 先判断添加的分类在同级是否存在同名情况
        List<Category> list = categoryMapper.selectByParentId(category.getParentId());
        if (list.stream()
                .anyMatch(item -> item.getName().equals(category.getName()))) {
            throw new FeelException("分类名重复，请重试");
        }
        categoryMapper.insert(category);
        return true;
    }

    @Override
    public Boolean del(Integer id) throws FeelException {
        // 删除操作， 先判断当前分类的状态，系统只有二级分类，先去查看是否为父分类
        Category category = categoryMapper.selectById(id);
        if (category.getParentId().equals(0) && category.getChildList().size() > 0) {
            throw new FeelException("当然父分类存在子分类，禁止删除");
        }
        // todo 如果为子分类，则需要查看当前分类先是否存在商品，存在则不允许删除
        if (!category.getParentId().equals(0) && false) {
            throw new FeelException("当然分类存在商品，禁止删除");
        }
        categoryMapper.delete(id);
        return true;
    }

    // 更新分类信息，主要检查同级是否引用、以及父分类不能自引用
    @Override
    public Boolean update(Category category) throws FeelException {
        // 通过id去查询数据库中的旧数据
        Category oldCategory = categoryMapper.selectById(category.getId());
        // 先对父分类Id进行检验
        if (category.getParentId() != null && category.getParentId() < 0) {
            throw new FeelException("错误请求，请修改");
        }
        if (category.getParentId() != null && oldCategory.getParentId().equals(0) && !category.getParentId().equals(0) && !oldCategory.getChildList().isEmpty()) {
            throw new FeelException("该分类有子分类，禁止修改成为子分类");
        }
        // 查看同级是否存在同名情况
        if (category.getName() != null || category.getParentId() != null) { // 判断是否对分类名称进行修改
            Integer parentId = category.getParentId() != null ? category.getParentId() : oldCategory.getParentId();
            String name = category.getName() != null ? category.getName() : oldCategory.getName();
            if (categoryMapper.selectByParentId(parentId)
                    .stream()
                    .anyMatch(item -> !item.getId().equals(category.getId()) && item.getName().equals(name))) {
                throw new FeelException("分类重名，请重试");
            }
        }

        // 判断标签是否循环引用
        if (category.getParentId() != null && category.getParentId().equals(category.getId())) {
            throw new FeelException("分类标签禁止自我依赖");
        }

        int i = categoryMapper.update(category);
        return i > 0;
    }

    @Override
    public Object getByCondition(Category condition, Integer pageNum, Integer pageSize) {
        if (pageNum == null) {
            List<Category> list = categoryMapper.selectByCondition(condition);
            list.forEach(item -> {
                ArrayList<Goods> goodsArrayList = new ArrayList<>();
                item.getChildList()
                        .forEach(child -> {
                            List<Goods> childGoodsList = goodsService.selectByCategoryId(child.getId());
                            child.setGoodsList(childGoodsList);
                            goodsArrayList.addAll(childGoodsList);
                        });
                item.setGoodsList(goodsArrayList);
            });
            return list;
        }
        // 开启分页
        PageHelper.startPage(pageNum, pageSize);
        List<Category> list = categoryMapper.selectByCondition(condition);
        PageInfo<Category> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public Category getById(Integer id) {
        return categoryMapper.selectById(id);
    }
}
